package com.etc.ssm.controller;

import com.etc.ssm.entity.User;
import com.etc.ssm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("api/user/{userid}")
    @ResponseBody
    public User listUser(@PathVariable("userid") int userid) {
        return userService.getUserById(userid);
    }

    @GetMapping("api/user/th/{userid}")
    public String listUser(@PathVariable("userid") int userid, Model model) {
        User user = userService.getUserById(userid);
        model.addAttribute("user", user);
        return "index";
    }

    @GetMapping("api/user/th")
    public String listUser(Model model) {
        List<User> users = userService.getUsers();
        model.addAttribute("users", users);
        return "userlist";
    }

    @GetMapping("user/login")
    public String login() {

        return "login";
    }

    @GetMapping("user/dologin")
    public String dologin() {

        return "back/index.html";
    }
}
